package com.yuba.front.controller.ai;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yuba.common.ai.utils.AiChatUtil;
import com.yuba.common.web.core.BaseController;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai/chat")
public class AiChatController extends BaseController {

    private final ChatClient chatClient;

    public AiChatController(ChatClient.Builder builder, ChatModel chatModel, VectorStore vectorStore) {
        this.chatClient = AiChatUtil.buildClient(builder, chatModel, vectorStore);
    }

    /**
     * 流式返回数据
     * @param input 输入内容
     * @return 数据流
     */
    @SaIgnore
    @GetMapping(value = "stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> streamChat(@RequestParam String input, @RequestParam String uuid) {
        return AiChatUtil.chatStream(chatClient, input);
    }

}
